//
//  PressCollectViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/20.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "PressCollectViewController.h"
#import "NewsDetailsViewController.h"
#import "FindNewsCollectionViewModel.h"
#import "MainNewsModel.h"
#import "NewsTableViewCell.h"
#import "FindNewsCollectionModel.h"
#import "MainNewsDetailsViewModel.h"
@interface PressCollectViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView* newsTable;
@property(nonatomic,strong)FindNewsCollectionViewModel * findNCViewModel;
@property(nonatomic,strong)MainNewsDetailsViewModel * mainNewsModel;
@property(nonatomic,strong)UIView * tempView;
@end

@implementation PressCollectViewController

-(FindNewsCollectionViewModel *)findNCViewModel{
    if (!_findNCViewModel) {
        _findNCViewModel = [[FindNewsCollectionViewModel alloc] init];
    }
    return _findNCViewModel;
}
-(MainNewsDetailsViewModel *) mainNewsModel{
    if (!_mainNewsModel) {
        _mainNewsModel = [[MainNewsDetailsViewModel alloc] init];
    }
    return _mainNewsModel;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav];
    [self initUI];
    [self setupRefresh];
    
    [self createTempView];
    // Do any additional setup after loading the view.
    
}
-(void)setupRefresh{
    
    self.newsTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(mj_headerRefresh)];
    [self.newsTable.mj_header beginRefreshing];
    
}

-(void)mj_headerRefresh{
    
    [self getData];
    
}

-(void)initNav{
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"新闻收藏"];
    
}
-(void)getData{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.findNCViewModel FindNewsCollectionParameter:UserInfoShare.userCenterModel.userID] subscribeNext:^(id result) {
        
        if([result[@"errorcode"] integerValue] == 0){
            
            if (self.findNCViewModel.newsList.count==0) {
                
                self.tempView.hidden = NO;
                
            }else{
                
                self.tempView.hidden = YES;
            }
            
        }
  
        [self.newsTable reloadData];
        [MXUtility hideMBProgress:self.view];
        [self.newsTable.mj_header endRefreshing];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
        [self.newsTable.mj_header endRefreshing];
    }];
    
}
-(void)createTempView{
    
    self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.newsTable.width, self.newsTable.height)];
    
    UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(DEF_DEVICE_WIDTH/2-25, 200, 50, 50)];
    imageview.image = [UIImage imageNamed:@"wushuju"];
    imageview.contentMode = UIViewContentModeScaleAspectFit;
    [self.tempView addSubview:imageview];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0,260, DEF_DEVICE_WIDTH, 30)];
    label.textColor = DEF_COLOR_RGB(215, 215, 215);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.f];
    label.text = @"你还没有收藏过新闻";
    [self.tempView addSubview:label];
    
    [self.newsTable addSubview:self.tempView];
    self.tempView.hidden = YES;
}

-(void)initUI{
    
    self.newsTable=[UITableView new];
    self.newsTable.delegate=self;
    self.newsTable.dataSource=self;
    self.newsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.newsTable];
    [self.newsTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(DEF_DEVICE_WIDTH);
        make.height.mas_equalTo(DEF_CONTENT_INTABBAR_HEIGHT - 34);
        make.top.equalTo(self.view).with.offset(0);
        make.leading.equalTo(self.view).with.offset(0);
    }];
    
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.findNCViewModel.newsList.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self tableView:tableView cellForRowAtIndexPath:indexPath].frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"findNewsCell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        
        cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.fNCModel=self.findNCViewModel.newsList[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    FindNewsCollectionModel* model=self.findNCViewModel.newsList[indexPath.row];
    //    新闻详情页
    NewsDetailsViewController* vc=[[NewsDetailsViewController alloc]init];
    vc.hidesBottomBarWhenPushed=YES;
    vc.newsId=model.newsId;
    vc.controllerFromWhere=UIViewControllerFromMine;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    CATransform3D transform3D = CATransform3DMakeRotation(M_PI_2, 0.0, 1.0, 1.0);
    cell.alpha = 0.0;
    cell.layer.transform = transform3D;
    cell.layer.anchorPoint = CGPointMake(0.0, 0.5); // 设置锚点位置；默认为中心点(0.5, 0.5)
    
    [UIView beginAnimations:@"transform" context:NULL];
    [UIView setAnimationDuration:0.5];
    cell.alpha = 1.0;
    cell.layer.transform = CATransform3DIdentity;
    CGRect rect = cell.frame;
    rect.origin.x = 0.0;
    cell.frame = rect;
    [UIView commitAnimations];
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FindNewsCollectionModel* model=self.findNCViewModel.newsList[indexPath.row];
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否删除收藏" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *Sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
            [MXUtility showMBProgress:self.view message:LoadingText];
            
            [[self.mainNewsModel DelNewsCollectionRequestpParameter:UserInfoShare.userCenterModel.userID NewsId:model.newsId] subscribeNext:^(id result) {
                
                if([result[@"errorcode"] integerValue] == 0){
                    
                    [MXUtility showTips:@"删除收藏成功"];
                    [self setupRefresh];
                }
                
                [MXUtility hideMBProgress:self.view];
            }error:^(NSError *error) {
                
                [MXUtility hideMBProgress:self.view];
                
            }];
            
        }];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:Sure];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
        
    }];
    
    return @[deleteRowAction];
}

@end
